<?php
declare (strict_types=1);

namespace app\user\logic\user\admin;

use app\admin\logic\system\Log;
use app\common\exception\LogicException;
use app\user\model\user\admin\VipUser as VipUserModel;

class VipUser extends VipUserModel
{
    /**
     * 会员列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-08-14
     *
     * @param array $map [description]
     * @return   [type]                              [description]
     */
    public function getList(array $hasWhere = [])
    {
        return $this->with(['user'])->alias('VipUser')->hasWhere('user', $hasWhere)->order('update_time', 'desc')->paginate($this->getListRows());
    }

    /**
     * 修改vip
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-08-14
     *
     * @param int   $id   [description]
     * @param array $data [description]
     * @return   [type]                               [description]
     */
    public function editVip(int $id, array $data)
    {
        $vip = $this->where('id', $id)->findOrFail();

        $vip->expire_time = $data['expire_time'];
        if (!$vip->save()) {
            throw new LogicException('修改失败');
        }

        $user = $vip->user;

        Log::write('修改会员【' . $user->nick_name . '，' . $user->phone . '】到期时间为' . $data['expire_time'], 'user_vip', 'update', true);

        return $vip;
    }

}
